chai = require 'chai'
chai.should()
sinon = require 'sinon'

{Refresh} = require "../src/refresh"
{TokenType} = require "../src/tokenType"
{MemoryRepository} = require "../src/memoryRepository"
should = require 'should'
describe "Validate token method", ->
  it 'should return null with expired token',(done) ->

    token = new Object()
    token.TokenExpiration = 1
    token.TokenType = TokenType.Refresh
    token.Id = 'badtokenstring'
    mockRepository = new MemoryRepository()
    mockRepository.set token.Id, token, ()->
      refresh = new Refresh(null, mockRepository)
      refresh.validateToken 'badtokenstring', (err, userId)->
        should.not.exist userId
        done()

  it 'should return userId with unexpired token',(done) ->
    token = new Object()
    token.TokenExpiration = Date.now() + 100000000
    token.TokenType = TokenType.Refresh
    token.UserId = "myUserId"
    token.Id = 'goodtokenstring'
    mockRepository = new MemoryRepository()
    mockRepository.set token.Id, token, ()->
      refresh = new Refresh(null, mockRepository)
      refresh.validateToken 'goodtokenstring', (err, userId)->
        console.log "userId: #{userId}"
        userId.should.equal 'myUserId'
        done()

  it 'should return null with null token', (done) ->
    mockRepository = new MemoryRepository()
    refresh = new Refresh(null,  mockRepository )
    refresh.validateToken 'someTokenString', (err, userId)->
      should.not.exist userId
      done()

  it 'should return null when token type is not refresh', (done)->
    token = new Object()
    token.Id = 'myTokenId'
    token.TokenExpiration = Date.now() + 100000000
    token.TokenType = TokenType.Access
    mockRepository = new MemoryRepository()
    mockRepository.set token.Id, token, (err)->
      refresh = new Refresh(null, mockRepository)
      refresh.validateToken token.Id, (err, userId)->
        should.not.exist userId
        done()
